from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

# 1. 获取数据
iris = load_iris()

# 2. 数据基本处理
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)

# 3. 特征工程
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)

# 4.机器学习-KNN
estimator = KNeighborsClassifier()
# 交叉验证，网格搜索
param_grid = {"n_neighbors":[1, 3, 5, 7]}
estimator = GridSearchCV(estimator, param_grid=param_grid, cv=5)


estimator.fit(x_train, y_train)

# 5. 模型评估
y_pre = estimator.predict(x_test )
print("预测值是：\n", y_pre)
print("预测值与真实值的对比是：\n", y_pre == y_test)
score = estimator.score(x_test, y_test)
print("准确率为：\n", score)

# 查看交叉验证，网格搜素的一些属性
print("在交叉验证中，得到的最好结果是：\n", estimator.best_score_)
print("在交叉验证中，得到的最好模型是：\n", estimator.best_estimator_)
print("在交叉验证中，得到的模型结果是：\n", estimator.cv_results_)
